package com.cskaoyan.service;

import com.cskaoyan.bean.Region;
import com.cskaoyan.bean.RegionExample;
import com.cskaoyan.bean.region.vo.CityBean;
import com.cskaoyan.bean.region.vo.CountyBean;
import com.cskaoyan.bean.region.vo.RegionVo;
import com.cskaoyan.bean.region.wxvo.WxRegionVo;
import com.cskaoyan.mapper.RegionMapper;

import java.util.ArrayList;
import java.util.List;

import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author wangdezhao
 * @date 2021/6/4
 * @description : admin/region/list
 */
@NoArgsConstructor
@Data
@Service

public class RegionServiceImpl implements RegionService {
    @Autowired
    RegionMapper regionMapper;

    public List<RegionVo> selectAllRegions() {

        RegionExample regionExample1 = new RegionExample();
        RegionExample regionExample2 = new RegionExample();
        RegionExample regionExample3 = new RegionExample();
        RegionExample.Criteria criteria1= regionExample1.createCriteria();
        RegionExample.Criteria criteria2= regionExample2.createCriteria();
        RegionExample.Criteria criteria3= regionExample3.createCriteria();
        criteria1.andTypeEqualTo((byte)1);
        criteria2.andTypeEqualTo((byte)2);
        criteria3.andTypeEqualTo((byte)3);

        // 所有省
        List<Region> provincePOJOs = regionMapper.selectByExample(regionExample1);
        // 所有市
        List<Region> cityPOJOs = regionMapper.selectByExample(regionExample2);
        // 所有县
        List<Region> countyPOJOs = regionMapper.selectByExample(regionExample3);

        List<RegionVo> regionVoList = new ArrayList<>();

        for (Region province : provincePOJOs) {
            RegionVo regionVo = new RegionVo();
            List<CityBean> cityVoList = new ArrayList<>();
            for (Region city : cityPOJOs) { // cityPOJO
                if(city.getPid() == province.getId()){

                    CityBean cityBean = new CityBean();
                    List<CountyBean> countyVoList = new ArrayList<>();

                    for (Region county : countyPOJOs) { // countyPOJO
                        if(county.getPid() == city.getId()){
                            CountyBean countyBean = new CountyBean();
                            countyBean.setId(county.getId());
                            countyBean.setName(county.getName());
                            countyBean.setType(county.getType());
                            countyBean.setCode(county.getCode());
                            countyVoList.add(countyBean);
                        }
                    }
                    // cityBean赋值
                    cityBean.setId(city.getId());
                    cityBean.setCode(city.getCode());
                    cityBean.setName(city.getName());
                    cityBean.setType(city.getType());
                    cityBean.setChildren(countyVoList);
                    cityVoList.add(cityBean);
                }
            }
            // regionVo赋值
            regionVo.setId(province.getId());
            regionVo.setName(province.getName());
            regionVo.setCode(province.getCode());
            regionVo.setType(province.getType());
            regionVo.setChildren(cityVoList);
            regionVoList.add(regionVo);
        }
     return regionVoList;
    }

    @Override
    public List<WxRegionVo> selectRegionByPid(Integer pid) {


        ArrayList<WxRegionVo> wxRegionVos = new ArrayList<>();
        RegionExample regionExample = new RegionExample();
        RegionExample.Criteria criteria = regionExample.createCriteria();
        criteria.andPidEqualTo(pid);

        List<Region> regions = regionMapper.selectByExample(regionExample);


        Integer type = regions.get(0).getType();
        if (type == 1) {
            regionsFor(wxRegionVos, regions);
        }
        if (type == 2) {
            regionsFor(wxRegionVos, regions);
        }
        if (type == 3) {
            regionsFor(wxRegionVos, regions);
        }
        return wxRegionVos;
    }

    private void regionsFor(ArrayList<WxRegionVo> wxRegionVos, List<Region> regions) {
        for (Region region : regions) {
            WxRegionVo wxRegionVo = new WxRegionVo();
            wxRegionVo.setCode(region.getCode());
            wxRegionVo.setId(region.getId());
            wxRegionVo.setName(region.getName());
            wxRegionVo.setPid(region.getPid());
            wxRegionVo.setType(region.getType());
            wxRegionVos.add(wxRegionVo);
        }
    }
}
